home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / RTANKSRC.ZIP / HOST.C < prev    next >
C/C++ Source or Header  |  1995-03-07  |  13KB  |  543 lines

  1. void sort_results(void);
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <graphics.h>
  6. #include "dtypes.h"
  7. #include <alloc.h>
  8. #include "keybd.h"
  9. #include <conio.h>
  10. #include <bios.h>
  11. #include <string.h>
  12.  
  13. #include "host.h"
  14. #include "loadpcx.h"
  15. #include "ginput.h"
  16. #include "newload.h"
  17. #include "callasm.h"
  18. #include "newterp.h"
  19. #include "bullet.h"
  20. #include "bullet.ext"
  21. #include "tiles.h"
  22. #include "code.h"
  23. #include "tss.h"
  24.  
  25. void view_manual(void);
  26. void init_manual_track();
  27. void inc_manual_track();
  28.  
  29.  
  30. #define SCORE struct score_type
  31.  
  32. SCORE {
  33.     int seconds;
  34.     int player;
  35.     int hits;
  36. };
  37.  
  38. SCORE res[11];
  39.  
  40. int chassis   = 1;
  41. int weapons[] = {0,0,0,0,0,0,0,0,0,0,0,0};
  42. int extras[]  = {0,0,0,0,0,0,0,0,0,0,0,0};
  43.  
  44. int px[] = {40,40,40,25,25,25,175,175,175,100};
  45. int py[] = {48,72,96,126,141,156,126,141,156,171};
  46.  
  47. TSS  tnk;
  48.  
  49. PSTR wind;
  50.  
  51. BOOL fileerr = FALSE;
  52.  
  53. int num_players;
  54. int terrain;
  55.  
  56. PLAYER players[10];
  57. p_TANK point[10];
  58. BOOL   update_scr;
  59. BOOL   cwe=TRUE;            /* Compiled with Errors */
  60. int    x,y;
  61.  
  62. /*-------------------------------------
  63.  * Function : void load_battle_specs(void)
  64.  * Purpose  : Load in all info required for battle from BATTLE.DAT
  65.  * Date     : 02/26/1989 18:01:01
  66.  */
  67. void load_battle_specs(void)
  68. {
  69. FILE *fp;
  70. int a,b, hand;
  71. TSS pl;
  72. p_TANK tp;
  73. char buf[20];
  74.  
  75.     x=5; y=3;
  76. /*
  77.     printf("Available ram DS = %u HEAP = %lu\n",coreleft(),(long) farcoreleft());
  78. */
  79.     if ((fp=fopen("BATTLE.DAT","rb"))==NULL) {
  80.         printf("Cannot open BATTLE.DAT for input\n");
  81.         printf("Run program from NEWSHOP.EXE \n");
  82.         exit(1);
  83.     }
  84.  
  85.     num_players=getw(fp);
  86.     terrain =getw(fp)+1;
  87.  
  88.     for (a=0; a<10; a++) {
  89.         players[a].inbattle=FALSE;
  90.         players[a].active=FALSE;
  91.         point[a]=NULL;
  92.     }
  93.  
  94. /*
  95.     printf("%d contestants, terrain %d\n",num_players,terrain);
  96. */
  97.     for (a=0; a<num_players; a++) {
  98.  
  99.        fread(&pl,sizeof(TSS),1,fp);
  100. /*
  101.        printf("%d. %s team %d\n",a+1,pl.name, pl.team);
  102. */
  103.        strcpy(players[a].tankname, pl.name);
  104.        players[a].chassisType = pl.t_chassis;
  105.        players[a].inbattle=TRUE;
  106.        players[a].active=TRUE;
  107.  
  108.        for (b=0; b<10; b++) {
  109.            players[a].weaponsType[b]=pl.t_weapons[b];
  110.            players[a].extrasType[b]=pl.t_extras[b];
  111.        }
  112.  
  113.        setcolor(BLACK);
  114.        sprintf(buf,"Compiling \"%s\"",pl.name);
  115.        note_error(buf);
  116.        setcolor(CGA_WHITE);
  117.  
  118.        hand= n_compile_player(&players[a]);
  119.        if (hand<0)
  120.            cwe=TRUE;
  121.  
  122.        players[a].tank_handle=hand;
  123.        tp=get_tank_pointer(players[a].tank_handle);
  124.        init_tank_image(tp,pl.team);
  125.        players[a].dteam=pl.team;
  126.     }
  127.  
  128.     fclose(fp);
  129.  
  130. } /* void load_battle_specs(void) */
  131.  
  132.  
  133. /*-------------------------------------
  134.  * Function : void main(void)
  135.  * Purpose  : Main calling module for HOST.C
  136.  * Date     : 02/19/1989 17:10:26
  137.  */
  138. void main(void)
  139. {
  140.  
  141.     num_players=0;
  142.     cwe=FALSE;
  143.  
  144.     register_bgi();
  145.  
  146.     set_screen_mode(4);
  147.  
  148.     Load_CGA_PCX("Z4400.B31");
  149.  
  150.     load_battle_specs();
  151.  
  152.     if (!cwe) {
  153.         show_error("TO START BATTLE,");
  154.         host_battle();
  155.     } else
  156.         getchar();
  157.  
  158.     closegraph();
  159. }
  160.  
  161.  
  162. /*-------------------------------------
  163.  * Function : void register_bgi(void)
  164.  * Purpose  : Register the BGI driver for CGA
  165.  * Date     : 02/19/1989 17:16:18
  166.  */
  167. void register_bgi(void)
  168. {
  169. int gm, gd;
  170.  
  171.     if (registerfarbgidriver(CGA_driver_far) < 0) {
  172.         printf("Color graphics adapter required for this program\n");
  173.         exit(1);
  174.     };
  175.  
  176.     gm=CGAC1;
  177.     gd=CGA;
  178.  
  179.     initgraph(&gd,&gm,"");
  180.  
  181.     if (graphresult()<0) {
  182.         printf("Graphics driver initilization error, CGA required\n");
  183.         exit(1);
  184.     }
  185. } /* void register_bgi(void) */
  186.  
  187. /*-------------------------------------
  188.  * Function : void open_wind(void)
  189.  * Purpose  : Open dialog box window
  190.  * Date     : 02/26/1989 12:53:21
  191.  */
  192. void open_wind(void)
  193. {
  194. int size;
  195.  
  196.    size=imagesize(70,70,250,160);
  197.  
  198.    if ((wind=malloc(size))==NULL) {
  199.        printf("Cannot Malloc()\n");
  200.        exit(1); /* Temp error */
  201.    }
  202.  
  203.    getimage(70,70,250,160,wind);
  204.  
  205.    setfillstyle(CLOSE_DOT_FILL,CGA_RED);
  206.    bar(70,70,250,160);
  207.    setfillstyle(SOLID_FILL,CGA_WHITE);
  208.    bar(80,80,240,150);
  209.    setcolor(BLACK);
  210.    rectangle(70,70,250,160);
  211.    rectangle(80,80,240,150);
  212.  
  213. } /* void open_wind(void) */
  214.  
  215. /*-------------------------------------
  216.  * Function : void close_wind(void)
  217.  * Purpose  : Close dialog box window
  218.  * Date     : 02/26/1989 12:57:05
  219.  */
  220. void close_wind(void)
  221. {
  222.     putimage(70,70,wind,COPY_PUT);
  223.     free(wind);
  224. } /* void close_wind(void) */
  225.  
  226. void show_error(char *s)
  227. {
  228.     open_wind();
  229.     moveto(85,100);
  230.  
  231.     outtext(s);
  232.     moveto(110,120);
  233.  
  234.     outtext("PRESS ENTER ");
  235.     ggetline(1,"   ");
  236.  
  237.     close_wind();
  238.  
  239. } /* void show_error(char *s) */
  240.  
  241.  
  242. /*<f>----------------------------------------
  243.  * FUNCTION: <s> void host_battle(void)
  244.  * PURPOSE : Monitor keyboard and host the COMBAT!!!!
  245.  *         :
  246.  * CREATION: 01/09/1989 11:44:56
  247.  */
  248. void host_battle(void)
  249. {
  250. p_TANK pl;
  251. int a,c,ch,z, monvar;
  252. BOOL cont;
  253. BOOL manual;
  254.  
  255.    randomize();
  256.    manual=FALSE;
  257.  
  258.    init_bullets();
  259.    v_startup_playfield(terrain);
  260.  
  261.    for (a=0; a<num_players; a++) {
  262.          point[a]=get_tank_pointer(players[a].tank_handle);
  263.          point[a]->time = biostime(0,0);
  264.          point[a]->died = point[a]->died;
  265.          init_interpreter(point[a]);
  266.          save_under_tank(point[a]);
  267.          point[a]->team=players[a].dteam;
  268.          point[a]->var[CMYTEAM]=point[a]->team;
  269.    }
  270.  
  271.    a=0; cont=TRUE; monvar=0;
  272.  
  273.    while (cont) {
  274.  
  275.       c=0;
  276.       while (!players[a].inbattle && c<num_players) {
  277.          a++; if (a==num_players) a=0;
  278.          c++;
  279.       }
  280.  
  281.       if (c==num_players)
  282.          cont=FALSE;
  283.       else {
  284.  
  285.          pl=point[a];
  286.  
  287.          set_move_range(pl);
  288.  
  289.          gotoxy( 8,21); printf("%2d",a+1);
  290.          gotoxy(25,21); printf("%-8s",players[a].tankname);
  291.          gotoxy( 8,22); printf("%3d",pl->nhits);
  292.          gotoxy(1,23);  printf("VAR[%02d]=%4d",monvar,pl->var[monvar]);
  293.  
  294.          capturescr();
  295.  
  296.          update_scr=TRUE;
  297.  
  298.          while (!kbhit()) {
  299.  
  300.             for (z=0; z<num_players; z++)
  301.                interprete(point[z],&players[z]);
  302.  
  303.             move_bullets();
  304.             check_for_hits();
  305.             move_bullets();
  306.             check_for_hits();
  307.  
  308.             if (update_scr) {
  309.                if (!manual)
  310.                   monitor_tank(pl);
  311.                else
  312.                   view_manual();
  313.             }
  314.  
  315.             update_scr=FALSE;
  316.          }
  317.  
  318.          if (!kbhit())
  319.             ch=' ';
  320.          else
  321.             ch=getkey();
  322.  
  323.          switch (ch) {
  324.             case ' ': a++;
  325.                       if (a>=num_players) a=0;
  326.                       manual=FALSE;
  327.                       break;
  328.  
  329.             case 27 : cont=FALSE;
  330.  
  331.             case '<':
  332.             case ',': monvar--; if (monvar<0) monvar=49;
  333.                       break;
  334.  
  335.             case '>':
  336.             case '.': monvar++; if (monvar>=50) monvar=0;
  337.                       break;
  338.  
  339.             case UP_ARR : if (!manual) {
  340.                              manual=TRUE;
  341.                              init_manual_track();
  342.                           }
  343.                           inc_manual_view(0,-1);
  344.                           break;
  345.  
  346.             case DN_ARR : if (!manual) {
  347.                              manual=TRUE;
  348.                              init_manual_track();
  349.                           }
  350.                           inc_manual_view(0,1);
  351.                           break;
  352.  
  353.             case LF_ARR : if (!manual) {
  354.                              manual=TRUE;
  355.                              init_manual_track();
  356.                           }
  357.                           inc_manual_view(-1,0);
  358.                           break;
  359.  
  360.             case RT_ARR : if (!manual) {
  361.                              manual=TRUE;
  362.                              init_manual_track();
  363.                           }
  364.               inc_manual_view(1,0);
  365.                           break;
  366.  
  367.             case '1':
  368.             case '2':
  369.             case '3':
  370.             case '4':
  371.             case '5':
  372.             case '6':
  373.             case '7':
  374.             case '8':
  375.             case '9': manual=FALSE;
  376.                       a=ch-'1';
  377.                       break;
  378.  
  379.             case '0': manual=FALSE;
  380.                       a=9;
  381.                       break;
  382.          }
  383.       }
  384.    }
  385.  
  386.    conclude_battle();
  387. } /* void host_battle(void) */
  388.  
  389. /*<f>----------------------------------------
  390.  * FUNCTION: <s> void check_for_hits(void)
  391.  * PURPOSE : Check if any bullets hit a tank or not
  392.  *         :
  393.  * CREATION: 01/10/1989 14:05:17
  394.  */
  395. void check_for_hits(void)
  396. {
  397. int a,b;
  398.  
  399.    if (nhits==0) return;
  400.  
  401.    for (b=0; b<nhits; b++)
  402.       for (a=0; a<num_players; a++)
  403.          if (players[a].active &&
  404.              (hits[b].hx==point[a]->px || hits[b].hx==point[a]->px+1) &&
  405.              (hits[b].hy==point[a]->py || hits[b].hy==point[a]->py+1)
  406.             ) {
  407.                point[a]->boom=TRUE;
  408.                point[a]->bwait=10;
  409.                if ((point[a]->nhits++) > 28) {
  410.                   players[a].active=FALSE;
  411.                   setmapXY(point[a]->px  ,point[a]->py  ,DEAD1A);
  412.                   setmapXY(point[a]->px+1,point[a]->py  ,DEAD1B);
  413.                   setmapXY(point[a]->px  ,point[a]->py+1,DEAD1C);
  414.                   setmapXY(point[a]->px+1,point[a]->py+1,DEAD1D);
  415.                   point[a]->died=biostime(0,0);
  416.                }
  417.                point[a]->hit=TRUE;
  418.             }
  419. } /* void check_for_hits(void) */
  420.  
  421. /*<f>----------------------------------------
  422.  * FUNCTION: <s> void conclude_battle(void)
  423.  * PURPOSE : List battle standings
  424.  *         :
  425.  * CREATION: 01/11/1989 18:41:34
  426.  */
  427. void conclude_battle(void)
  428. {
  429. int a;
  430. char steam[10];
  431. char buf[80];
  432. DWORD t;
  433.  
  434.    t=biostime(0,0);
  435.  
  436.    Load_CGA_PCX("YXZ23.CXA");
  437.    gotoxy(1,1);
  438.  
  439.    for (a=0; a<num_players; a++) {
  440.       if (players[a].active)
  441.           point[a]->died=t;
  442.  
  443.       point[a]->secs = (point[a]->died-point[a]->time) / 18;
  444.       if (point[a]->secs<0)
  445.         point[a]->secs = (65535+point[a]->died-point[a]->time) /18;
  446.  
  447.       res[a].seconds = point[a]->secs;
  448.       res[a].player  = a;
  449.       res[a].hits    = point[a]->nhits;
  450.    }
  451.  
  452.    sort_results();
  453.  
  454.    for (a=0; a<num_players; a++) {
  455.  
  456.       moveto(px[a],py[a]);
  457.  
  458.       if (players[a].inbattle) {
  459.  
  460.          if (players[res[a].player].active)
  461.              setcolor(BLACK);
  462.          else setcolor(CGA_RED);
  463.  
  464.          switch(players[res[a].player].dteam) {
  465.             case ALLOUT : strcpy(steam,"N"); break;
  466.             case TEAMA  : strcpy(steam,"A"); break;
  467.             case TEAMB  : strcpy(steam,"B"); break;
  468.          }
  469.  
  470.          if (a<3)
  471.              sprintf(buf,"%8s %3d %s %02d:%02d",
  472.                       players[res[a].player].tankname,
  473.                       point[res[a].player]->nhits,
  474.                       steam,
  475.                       res[a].seconds / 60,
  476.                       res[a].seconds % 60
  477.                     );
  478.          else
  479.              sprintf(buf,"%8s %s %02d:%02d",
  480.                       players[res[a].player].tankname,
  481.                       steam,
  482.                       res[a].seconds / 60,
  483.                       res[a].seconds % 60
  484.                     );
  485.  
  486.          outtext(buf);
  487.       }
  488.    }
  489.  
  490.     while (!kbhit())
  491.         ;
  492.     (void) getch();
  493.  
  494. } /* void conclude_battle(void) */
  495.  
  496. /*-------------------------------------
  497.  * Function : void note_error(char *s)
  498.  * Purpose  : Display a line of graphical text
  499.  * Date     : 02/26/1989 20:25:24
  500.  */
  501. void note_error(char *s)
  502. {
  503.     outtextxy(x,(y++)*9,s);
  504.     if (y>21) {
  505.         moveto(300,185); ggetline(1,"   ");
  506.         setfillstyle(SOLID_FILL,CGA_RED);
  507.         bar(5,25,310,197);
  508.         y=3;
  509.     }
  510. } /* void note_error(char *s) */
  511.  
  512. /*-------------------------------------
  513.  * Function : void sort_results(void)
  514.  * Purpose  : Sort player results based on time in the ring
  515.  * Date     : 03/01/1989 14:39:34
  516.  */
  517. void sort_results(void)
  518. {
  519. int a,b, limit;
  520. SCORE t;
  521. BOOL witch=TRUE;
  522.  
  523.     limit=num_players;
  524.  
  525.     while (witch && limit>0) {
  526.         witch=FALSE;
  527.         for (a=0; a<limit-1; a++) {
  528.             if (
  529.                     (res[a].seconds < res[a+1].seconds) || /* shorter time */
  530.                     (
  531.                      (res[a].seconds == res[a+1].seconds) && /* fewer hits */
  532.                      (res[a].hits > res[a+1].hits)
  533.                     )
  534.                ) {
  535.                 t=res[a];
  536.                 res[a]=res[a+1];
  537.                 res[a+1]=t;
  538.                 witch = a;
  539.             }
  540.         }
  541.         limit--;
  542.     }
  543. } /* void sort_results(void) */